package com.cci.kangdao.service.impl;

import com.cci.kangdao.dao.EngineMsgDao;
import com.cci.kangdao.dao.ServiceStationDao;
import com.cci.kangdao.dao.WorkOrderTDao;
import com.cci.kangdao.dao.model.ServiceStationT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dao.model.WorkOrderT;
import com.cci.kangdao.service.DBUMessageService;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.SmsUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/12/27
 */
@Service
public class DBUMessageServiceImpl implements DBUMessageService {

    @Resource
    private WorkOrderTDao workOrderTDao;
    @Resource
    private ServiceStationDao serviceStationDao;
    
    @Autowired
    private EngineMsgDao engineMsgDao;

    @Override
    public String sendAssignMessage(UserT userT, WorkOrderT workOrderT) {
        StringBuffer sb = new StringBuffer();
        sb.append("派工：工单编号").append(workOrderT.getOrderNo()).append("，");
        if(workOrderT.getOrderChannel() == 1){
            sb.append("属于\"" + workOrderT.getSellType() + "\"" + (StringUtils.isNotBlank(workOrderT.getServiceSubtype()) ? "-" + workOrderT.getServiceSubtype() : "")).append("，");
        }
        sb.append(workOrderT.getLocProvince()).append("-").append(workOrderT.getLocCity()).append("，");
        Integer customerType = workOrderT.gCustomerTypeInteger();
        if(null != customerType){
            if(customerType.equals(0)){
                sb.append("个人，");
            }else{
                sb.append("企业（").append(workOrderT.getCustomerCompanyName()).append("）");
            }
        }
        sb.append(workOrderT.getCustomerName()).append("，电话：").append(workOrderT.getCustomerPhone()).append("，");
        sb.append("ESN：").append(workOrderT.getESN()).append("，OEM：").append(workOrderT.getoEMName());
        sb.append("，").append(workOrderT.getMarketType()).append("/").append(workOrderT.getApplication()).append("/").append(workOrderT.getWarrantyStartDate());
        sb.append("，描述：").append(workOrderT.getCustomerComplaint());
        if(null != workOrderT.getGuessDistance()){
            sb.append("，距离：").append(workOrderT.getGuessDistance()).append("km");;

        }
        sb.append("，报修时间：");
        if(StringUtils.isNotEmpty(workOrderT.getUploadTime())){
            sb.append(workOrderT.getUploadTime());
        }else{
            sb.append(workOrderT.getCreateTime());
        }
        SmsUtils.sendMessage(userT.getPhone(),sb.toString());
        return sb.toString();
    }

    @Override
    public String sendAssignMessage(UserT userT, Long orderId) {
        WorkOrderT workOrderT = workOrderTDao.getWorkOrderTByOrderId(orderId);
        return sendAssignMessage(userT,workOrderT);
    }

    @Override
    public String getUpgradeMessage(Long orderId) {
        WorkOrderT workOrderT = workOrderTDao.getWorkOrderTByOrderId(orderId);
        if(null != workOrderT){
            StringBuffer sb = new StringBuffer();
            sb.append("工单编号").append(workOrderT.getOrderNo()).append("，");
            Long serviceStationId = workOrderT.getServiceStationId();
            if(NumberUtils.isPositiveNum(serviceStationId)){
                ServiceStationT serviceStation = serviceStationDao.findServiceStationByID(String.valueOf(serviceStationId));
                sb.append(serviceStation.getStationname()).append("，");
            }
            sb.append(workOrderT.getLocProvince()).append("-").append(workOrderT.getLocCity()).append("，");
            Integer customerType = workOrderT.gCustomerTypeInteger();
            if(null != customerType){
                if(customerType.equals(0)){
                    sb.append("个人，");
                }else{
                    sb.append("企业（").append(workOrderT.getCustomerCompanyName()).append("）");
                }
            }
            sb.append(workOrderT.getCustomerName()).append("，电话：").append(workOrderT.getCustomerPhone()).append("，");
            sb.append("ESN：").append(workOrderT.getESN()).append("，");
            sb.append(workOrderT.getMarketType()).append("/").append(workOrderT.getApplication()).append("/").append(workOrderT.getWarrantyStartDate());
            sb.append("，描述：").append(workOrderT.getCustomerComplaint());
            if(null != workOrderT.getGuessDistance()){
                sb.append("，距离：").append(workOrderT.getGuessDistance()).append("km");;

            }
            sb.append("，报修时间：");
            if(StringUtils.isNotEmpty(workOrderT.getUploadTime())){
                sb.append(workOrderT.getUploadTime());
            }else{
                sb.append(workOrderT.getCreateTime());
            }
            return sb.toString();
        }
        return null;
    }
    
    private static String upgradeMessageForUnfinishedWorkOrder = "工单%d小时未完成超时,工单编号:%s,服务商名称:%s,故障描述:%s,ESN:%s,发动机型号:%s,主机厂:%s,故障地点:%s,工单状态:%s,报修时间:%s";
    
    /**
     * 创建时间：2021-04-16
     * 服务工单未完成超时提醒，需求变更后增加的方法
     */
    @Override
	public String getUpgradeMessageForUnfinishedWorkOrder(int timoutHour, Map<String,Object> overTimeOrder) {
         if(null != overTimeOrder){
        	 // 工单编号
        	 String orderNo = MapUtils.getString(overTimeOrder,"OrderNo");
        	 // esn
             String esn = MapUtils.getString(overTimeOrder,"ESN");
             // 主机厂名称
             String oEMName = MapUtils.getString(overTimeOrder,"OEMName", "");
             // 工单状态
             Integer orderStatus = MapUtils.getInteger(overTimeOrder,"OrderStatus");
             // 服务站id
             Long serviceStationId = MapUtils.getLong(overTimeOrder,"ServiceStationId");
             // 客户抱怨
             String customerComplaint = MapUtils.getString(overTimeOrder,"CustomerComplaint","");
             // 故障地点
             String location = MapUtils.getString(overTimeOrder,"Location","");
             // 报修时间
             String createTime = MapUtils.getString(overTimeOrder,"CreateTime","");
             
             // 根据esn查询出对应的发动机型号
             String engineType = engineMsgDao.getEngineTypeByESN(esn);
             
             String stationName = "无";
             if(NumberUtils.isPositiveNum(serviceStationId)){
                 ServiceStationT serviceStation = serviceStationDao.findServiceStationByID(String.valueOf(serviceStationId));
                 stationName = serviceStation.getStationname();
             }
             
             
            return String.format(upgradeMessageForUnfinishedWorkOrder, 
            					 							timoutHour,   // 几小时未完成超时
            					 							   orderNo,   // 工单编号
            					 						   stationName,   // 服务站名称
            					 					 customerComplaint,   // 故障描述
            					 					 			   esn,   // esn
            					(engineType == null ? "" : engineType),	  // 发动机型号
            					 					 		   oEMName,   // 主机厂
            					 					 		  location,   // 故障地点
            					 	     getOrderStatusCH(orderStatus),   // 工单状态
            					 	     					createTime	  // 报修时间
            					 							);
         }
         return "";
	}
    
    /**
     * 获取状态码对应的中文
     * @return
     */
    private String getOrderStatusCH(int orderStatus) {
    	//(4,6,7,8,9,10,11,12,18)
    	switch (orderStatus) {
		    	case 4:		return "待服务商接单";
		    	case 6:		return "待派服务技师";
		    	case 7:		return "待服务技师接单";
		    	case 8:		return "技师已接单";
		    	case 9:		return "技师已拒单";
		    	case 10:	return "已出发";
		    	case 11:	return "已到达";
		    	case 12:	return "维修中";
		    	case 18:	return "待区域工程师处理";
		}
    	return "";
    }
}
